﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IsmsExplorer.Model.Projects;
using IsmsExplorer.IsmsDataImport.UI.Common;
using IsmsExplorer.Application;
using IsmsExplorer.Application.Projects;

namespace IsmsExplorer.IsmsDataImport.UI {
    public partial class SelectProjectForm : Form {
        public SelectProjectForm() {
            InitializeComponent();

            cbProjects.DisplayMember = "Name";
            cbProjects.DropDownStyle = ComboBoxStyle.DropDownList;
        }

        private void SelectProjectForm_Load(object sender, EventArgs e) {
            try {
                Cursor = Cursors.WaitCursor;
                IList<Project> projects = ProjectService.GetAllProjects();
                if (projects.Count > 0) {
                    cbProjects.DataSource = new BindingList<Project>(projects);

                } else {
                    UIHelper.ShowInfoMessage(this, "No Projects in database");
                }

            } catch (Exception ex) {
                UIHelper.ShowErrorMessage(this, ex.Message);

            } finally {
                Cursor = Cursors.Default;
            }
        }

        private void btnOK_Click(object sender, EventArgs e) {
            if (cbProjects.SelectedItem != null) {
                UserSession.CurrentProject = cbProjects.SelectedItem as Project;
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e) {
            Close();
        }
    }
}
